<template>
  <div class="todo-list">
    <h2>待办事项列表</h2>
    <input v-model="newTodoTitle" @keyup.enter="addTodo" placeholder="添加新的待办事项" />
    <button @click="addTodo">添加</button>
    <ul>
      <TodoItem
        v-for="(todo, index) in todos"
        :key="index"
        :title="todo.title"
        :completed="todo.completed"
        @update:completed="updateTodoCompleted(index, $event)"
        @remove-todo="removeTodo(index)"
      />
    </ul>
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  name: 'TodoList',
  components: {
    TodoItem
  },
  data() {
    return {
      todos: [],
      newTodoTitle: ''
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    }
  }
}
</script>

<style scoped>
.todo-list {
  max-width: 400px;
  margin: 0 auto;
  text-align: left;
}

input {
  width: calc(100% - 110px);
  padding: 5px;
  margin-right: 5px;
}

button {
  padding: 5px 10px;
}

ul {
  list-style-type: none;
  padding: 0;
}

.todo-item {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.todo-item span {
  flex-grow: 1;
  margin-left: 5px;
}
</style>